package user

import (
	"errors"
	"fmt"
	"gitee.com/binny_w/gin_starter/model/rsa"
	"gitee.com/binny_w/go-util"
	util2 "gitee.com/binny_w/go-util/v2"
	"github.com/gin-gonic/gin"
	"time"
)

var ErrWrongPassSize = errors.New("密码长度6至20位")

func updateFixer(c *gin.Context, row *util2.MysqlRow) error {
	if row.IsEmpty() {
		return fmt.Errorf("数据为空")
	}
	if row.ToInt64("deleted_at") > 0 {
		row.Set("deleted_at", time.Now().Unix())
	}
	if pass := row.ToStr("pasd"); pass != "" {
		var err error
		if pass, err = rsa.Decode(pass); err != nil {
			return err
		} else if len(pass) < 6 || len(pass) > 20 {
			return ErrWrongPassSize
		} else if pass, err = util.StrHashPass(pass); err != nil {
			return err
		} else {
			row.Set("pasd", pass)
		}
	}
	if row.Has("role") {
		c.Set("new_role", row.ToStr("role"))
	}
	return nil
}
